﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageOrderTable.Command
{
    public class AddOrderInfoCommand : IRequest<Json<string>>
    {
        public string membername { get; set; }
        /// <summary>
        /// 设备id
        /// </summary>
        public int deviceid { get; set; }
        /// <summary>
        /// 出酒量 单位 ml
        /// </summary>
        public int num { get; set; }
        /// <summary>
        /// 出酒量文本：如   1两  1.5斤
        /// </summary>
        public string numName { get; set; }
        /// <summary>
        /// 价格
        /// </summary>
        public int price { get; set; }
        /// <summary>
        /// 来源
        /// </summary>
        public string source { get; set; }
    }

    public class AddOrderInfoCommandHandler : IRequestHandler<AddOrderInfoCommand, Json<string>>
    {
        private readonly IDevicetableRepository _idevicetableRepository;
        private readonly IordertableRepository _iorderRepository;
        private readonly ICurrentUserService _icurrentUserService;
        private readonly IskutableRepository _iskutableRepository;
        private readonly ICheckWineResponse _icheckwineResponse;
        public AddOrderInfoCommandHandler(IDevicetableRepository idevicetableRepository, IordertableRepository iorderRepository, ICurrentUserService icurrentUserService, IskutableRepository iskutableRepository, ICheckWineResponse icheckwineResponse)
        {
            _idevicetableRepository = idevicetableRepository;
            _iorderRepository = iorderRepository;
            _icurrentUserService = icurrentUserService;
            _iskutableRepository = iskutableRepository;
            _icheckwineResponse = icheckwineResponse;
        }
        public async Task<Json<string>> Handle(AddOrderInfoCommand request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserService.GetUserId();
            if (userid <= 0)
            {
                return new Json<string>() { Status = 1003, Detail = "请先登录" };
            }
            else if (request.deviceid <= 0)
            {
                return new Json<string>() { Status = 1003, Detail = "请选择出酒设备" };
            }
            else if (request.num <= 0)
            {
                return new Json<string>() { Status = 1003, Detail = "请选择出酒量" };
            }
            else
            {
                if ((await _iorderRepository.GetOrderCountByDeviceid(request.deviceid, cancellationToken)) > 0)
                {
                    return new Json<string>() { Status = 1003, Detail = "当前设备存在未完成的订单" };
                }
                else
                {
                    var model = await _idevicetableRepository.GetDevicetableByid(request.deviceid, cancellationToken);
                    if (model != null && model.id > 0)
                    {
                        if (model.skuid <= 0)
                        {
                            return new Json<string>() { Status = 1003, Detail = "当前设备请先绑定产品" };
                        }
                        var product = await _iskutableRepository.GetSkuTableById(model.skuid, cancellationToken);
                        if (!(product != null && product.id > 0))
                        {
                            return new Json<string>() { Status = 1003, Detail = "当前设备请先绑定有效产品" };
                        }
                        else if ((await _icheckwineResponse.SendOrderGetStatus(model.deviceid, cancellationToken)) != 3)
                        {
                            return new Json<string>() { Status = 1003, Detail = "设备已离线，请先启动设备" };
                        }
                        ordertable order = new ordertable();
                        order.userid = userid;
                        order.userpid = userid;
                        order.member = 0;
                        order.createtime = DateTime.Now;
                        order.updatetime = DateTime.Now;
                        order.ordercapacity = request.num;
                        order.currentcapacity = 0;
                        order.currentcapacitys = request.numName;
                        order.deviceid = request.deviceid;
                        order.skuid = model.skuid;
                        order.operationstate = 0;
                        order.name = product.name;
                        order.price = product.price * (request.num / 50);
                        order.unit = product.unit;
                        order.remark = "";
                        order.cusdeviceid = model.cusdeviceid;
                        await _iorderRepository.AddOrderInfo(order, cancellationToken);
                        await _iorderRepository.SendWine(model.deviceid, request.num, cancellationToken);

                        return new Json<string>() { Status = 1000, Detail = "操作成功" };
                    }
                    else
                    {
                        return new Json<string>() { Status = 1003, Detail = "未找到选择设备" };
                    }
                }
            }
        }
    }
}
